package com.thf.controller.news;

import com.alibaba.fastjson.JSON;
import com.thf.model.ResultData;
import com.thf.model.news.ImgModel;
import com.thf.util.ResultTool;
import com.thf.util.Upload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class NewUploadController{
    @RequestMapping("/uploadLui")
    @ResponseBody
    protected String newUpload(HttpServletRequest req, HttpServletResponse resp , @RequestParam("file") MultipartFile file) throws ServletException, IOException {
        String path = Upload.file(file,req);
        ResultData result = null ;
        if(!"".equals(path)){
            result = new ResultData(ResultTool.CODE_SUCCES,"",new ImgModel("http://localhost:8081"+path));
        }else{
            result = new ResultData(ResultTool.CODE_ERROR);
        }
        //       反馈给前端--返回值
        String resJson = JSON.toJSONString(result);
        //System.out.println(resJson);
//        resp.getWriter().println(resJson);
        return resJson;
    }
}
